<html><head>
<meta http-equiv="content-type" content="text/html; charset=windows-1252"></head><body>Using folding in scintilla<br>
scintilla is a script editing component http://www.scintilla.org for more information.<br>

<br>
<hr>

This setting up and using code-folding scintilla<br>

<hr>

<br><u>First we create some constants for later use</u><br>
<pre>static const int MARGIN_SCRIPT_FOLD_INDEX = 1;
</pre>

<br><u>Register the on margin click event for the window (this is windows specific)</u><br>

<pre>static const int <i>WINDOW_ID</i> = <i>900</i>;

BEGIN_MESSAGE_MAP(<i>CDocumentWindow</i>, <i>CDocumentWindowsBaseClass</i>)
  ON_NOTIFY(SCN_MARGINCLICK, <i>WINDOW_ID</i>, OnMarginClicked)
END_MESSAGE_MAP()
</pre>


<br>(WINDOW_ID is used in the CreateWindow call when you create the window)<br>

<br><u>Set the lexer you want to use...</u><br>
(This will be done in the constructor or on create method...)<br>

<pre>  SendEditor(SCI_SETLEXER, SCLEX_CPP);
  SendEditor(SCI_SETSTYLEBITS, 5);
</pre>

<br><u>Set some properties so the lexer knows what we want</u><br>

<pre>  SendEditor(SCI_SETPROPERTY, (WPARAM)"fold", (LPARAM)"1");
  SendEditor(SCI_SETPROPERTY, (WPARAM)"fold.compact", (LPARAM)"0");
</pre>

<br>
(The fold.compact option folds blank lines,<br>
which I don't like, but by default it's on "1")<br>

<br>I also like:<br>

<pre>  SendEditor(SCI_SETPROPERTY, (WPARAM)"fold.comment", (LPARAM)"1");
  SendEditor(SCI_SETPROPERTY, (WPARAM)"fold.preprocessor", (LPARAM)"1");
</pre>

<br><u>Now resize all the margins to zero</u><br>
(This will be done in a RecalcLineMargin method...)<br>

<pre>  SendEditor(SCI_SETMARGINWIDTHN, MARGIN_SCRIPT_FOLD_INDEX, 0);
</pre>

<br><u>Then set the margin type and margin mask and resize it...</u><br>

<pre>  SendEditor(SCI_SETMARGINTYPEN,  MARGIN_SCRIPT_FOLD_INDEX, SC_MARGIN_SYMBOL);
  SendEditor(SCI_SETMARGINMASKN, MARGIN_SCRIPT_FOLD_INDEX, SC_MASK_FOLDERS);
  SendEditor(SCI_SETMARGINWIDTHN, MARGIN_SCRIPT_FOLD_INDEX, 20);
</pre>

<br><u>Set some visual preferences</u><br>

<pre>  SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDER, SC_MARK_PLUS);
  SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDEROPEN, SC_MARK_MINUS);
  SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDEREND, SC_MARK_EMPTY);
  SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDERMIDTAIL, SC_MARK_EMPTY);
  SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDEROPENMID, SC_MARK_EMPTY);
  SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDERSUB, SC_MARK_EMPTY);
  SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDERTAIL, SC_MARK_EMPTY);

  SendEditor(SCI_SETFOLDFLAGS, 16, 0); // 16  	Draw line below if not expanded
</pre>

<br><u>Tell scintilla we want to be notified about mouse clicks in the margin</u><br>

<pre>  SendEditor(SCI_SETMARGINSENSITIVEN, MARGIN_SCRIPT_FOLD_INDEX, 1);
</pre>

<br><u>Respond to SCN_MARGINCLICK events...  (note that the method prototype is windows specific)</u><br>

<pre>afx_msg void
<i>CDocumentWindow</i>::OnMarginClick(NMHDR* nmhdr, LRESULT* result)
{
  SCNotification* notify = (SCNotification*)nmhdr;

  const int modifiers = notify-&gt;modifiers;
  const int position = notify-&gt;position;
  const int margin = notify-&gt;margin;
  const int line_number = SendEditor(SCI_LINEFROMPOSITION, position, 0);

  switch (margin) {
    case MARGIN_SCRIPT_FOLD_INDEX:
    {
      SendEditor(SCI_TOGGLEFOLD, line_number, 0);
    }
    break;
  }
}
</pre>

<br>
That should be enough to get it working.<br>
You'll want to play around with it until you're happy with the way the folding works,<br>
scintilla seems to use a much more complicated MarginClick...<br>
Luckily harvesting the MarginClick code from scintilla is easy:<br>

<br><u>First change in OnMarginClick change:</u><br>

<pre>      SendEditor(SCI_TOGGLEFOLD, line_number, 0);
</pre>

To:

<pre>      MarginClick(position, modifiers);
</pre>

<br><u>Then in your header file...</u><br>

<pre>  bool MarginClick(int position, int modifiers);
  void Expand(int &amp;line, bool doExpand,
              bool force = false, int visLevels = 0, int level = -1);
  void FoldAll();
</pre>

<br>
Then go and find those methods from SciTEBase.cxx and copy them to your code.<br>

<br><u>Oh, and you had better go update your Goto Line function now...</u><br>

<pre>	SendEditor(SCI_ENSUREVISIBLEENFORCEPOLICY, line_number);
	SendEditor(SCI_GOTOLINE, line_number);
</pre>

<br>
<br>


That's it, enjoy.<br>
You can send any comments or suggestions about this to vascy@hotmail.com<br>
<br>
<br>


</body></html>
